package cn.train.controler;

import java.io.IOException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.train.model.CheXiang;

import cn.train.service.AddOrderTableService;
import cn.train.service.QueryCXService;

@WebServlet("/UpdateOrderTabelStateServlet")
public class UpdateOrderTabelStateServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session=req.getSession();
		String xinxinum= (String)session.getAttribute("xinxinum");
		String onum= (String)session.getAttribute("onum");
		String piaotype= (String)session.getAttribute("zuotype");
		String piaotype1= (String)session.getAttribute("zuotype1");
		String shuliang= (String)session.getAttribute("renshu");
		String cname=(String)session.getAttribute("cname");
		String cname1=(String)session.getAttribute("cname1");
		String date= (String)session.getAttribute("actiondate");
		String date2= (String)session.getAttribute("actiondate1");
		System.out.println(date);
		System.out.println(date2);
	
		AddOrderTableService addts=new AddOrderTableService();
		QueryCXService queryche=new QueryCXService();
		boolean flag=addts.updateOrderTableState(onum);
		SimpleDateFormat adf = new SimpleDateFormat("yyyy-MM-dd");
	     int shu=Integer.parseInt(shuliang);
	     System.out.println("票数+++++"+shu);
	 	String chexiangzuohao="";
	 	String chexiangzuohao1="";
	 	try {
			java.util.Date date1 = adf.parse(date);
			java.sql.Date sqlDate=new java.sql.Date(date1.getTime());
			
			//先查票数piaoshu
			int piaoshu=addts.HuoQuPiao(cname, piaotype, sqlDate);
			//再分车厢车座
			//商务座的车厢和座号
			System.out.println("单程票数"+piaoshu);
			for(int i=1;i<=shu;i++){
				
			if("商务座".equals(piaotype)){
			
				
					piaoshu++;
		
				List<CheXiang> listshang=queryche.QuerySwid(piaoshu, piaoshu);
				req.setAttribute("listshang", listshang);
				for(CheXiang c:listshang){
					chexiangzuohao+=c.getChexianghao()+"车"+c.getZuohao()+"座-";
					System.out.println(c.getChexianghao()+";"+c.getZuohao());
				}

				
			}
			//普通座的车厢和座号
			if("普通座".equals(piaotype)){
				
					piaoshu++;
				
				List<CheXiang> listpu=queryche.QueryPtid(piaoshu, piaoshu);
		
			   for(CheXiang c:listpu){
				   chexiangzuohao+=c.getChexianghao()+"车"+c.getZuohao()+"座-";
					System.out.println(c.getChexianghao()+";"+c.getZuohao());
				}

			  
			   req.setAttribute("listpu", listpu);
			}
			//无座的车厢
			if("无座".equals(piaotype)){
				
					piaoshu++;
				
				int wuzuo=queryche.QueryZhid(piaoshu);
				chexiangzuohao+=wuzuo+"-";
			  req.setAttribute("wuzuo", wuzuo);
			  System.out.println(wuzuo);

			}
			}
			 chexiangzuohao=chexiangzuohao.substring(0,chexiangzuohao.length()-1);
			   req.setAttribute("chexiangzuohao", chexiangzuohao);
			System.out.println("单程"+chexiangzuohao);
			System.out.println("一共有"+shu);
			//后改数据库中票量
			int flag1=addts.panDuanPiao(piaotype, shu,sqlDate,cname);
			  System.out.println(flag1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
if("往返".equals(xinxinum)){
	 		try {
				java.util.Date date1 = adf.parse(date2);
				java.sql.Date sqlDate=new java.sql.Date(date1.getTime());
				
				//先查票数piaoshu
				int piaoshu=addts.HuoQuPiao(cname1, piaotype1, sqlDate);
				System.out.println("往返票数"+piaoshu);
				//再分车厢车座
				//商务座的车厢和座号
			
				for(int i=1;i<=shu;i++){
					
				if("商务座".equals(piaotype1)){
				
					
						piaoshu++;
				
					List<CheXiang> listshang=queryche.QuerySwid(piaoshu, piaoshu);
					req.setAttribute("listshang", listshang);
					for(CheXiang c:listshang){
						chexiangzuohao1+=c.getChexianghao()+"车"+c.getZuohao()+"座-";
						System.out.println(c.getChexianghao()+";"+c.getZuohao());
					}

					//piaoshu++;

					
				}
				//普通座的车厢和座号
				if("普通座".equals(piaotype1)){
		
						piaoshu++;
			
					List<CheXiang> listpu=queryche.QueryPtid(piaoshu, piaoshu);
			
				   for(CheXiang c:listpu){
					   chexiangzuohao1+=c.getChexianghao()+"车"+c.getZuohao()+"座-";
						System.out.println(c.getChexianghao()+";"+c.getZuohao());
					}

	

				  
				   req.setAttribute("listpu", listpu);
				}
				//无座的车厢
				if("无座".equals(piaotype1)){
					
						piaoshu++;
				
					int wuzuo=queryche.QueryZhid(piaoshu);
					chexiangzuohao1+=wuzuo+"-";
				  req.setAttribute("wuzuo", wuzuo);
				  System.out.println(wuzuo);

				}
				}
				 chexiangzuohao1=chexiangzuohao1.substring(0,chexiangzuohao1.length()-1);
				   req.setAttribute("chexiangzuohao1", chexiangzuohao1);
				System.out.println("往返"+chexiangzuohao1);
				//后改数据库中票量
				System.out.println("一共有"+shu);
				int flag1=addts.panDuanPiao(piaotype1, shu,sqlDate,cname1);
				  System.out.println(flag1);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	 	}
		
		if (flag) {
			req.getRequestDispatcher("font/pay/pay_succ.jsp").forward( req,resp);
		}
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

